$(document).ready(function(e) {
    $(".contnet_list ul li").mouseover(function(){
		$(this).css("background", "#FFF");
	});
    $(".contnet_list ul li").mouseout(function(){
		$(this).css("background", "#9CF");
	});
});

$(document).ready(function(e) {
    $(".news_cancel").click(function(){
		clear();
	});
});

$(document).ready(function(e) {
    $(".img_cancel").click(function(){
		$(".select_img").fadeOut("fast");
		$(".img_list").html('');
	});
});

/******************************* 选择图片 **************************************/
$(document).ready(function(e) {
    $("#news form input[name='select_img']").click(function(){
		$(".select_img").fadeIn("fast");
		var p_id = $("#news form input[name='p_id']").val();
		$.ajax({
			type:"GET",
			url:"getimg",
			data:"p_id=" + p_id,
			dataType:"json",
			beforeSend: function(){},
			success: function(data){
				for(var i=0; i < data.total; i++){
					//$(".img_lists_content").live("click", function(){});
					$(".img_list").append("<div class='img_lists_content' name='" + data.json[i].img + "'><img src='" + data.json[i].preview +"' /></div>");
				}
				$(".img_lists_content").bind("click", function(){
					var url = $(this).attr('name');
					$("#news form input[name='image']").val(url);
					$(".select_img").fadeOut("fast");
					$(".img_list").html('');
				});
			}
		});
	});
});

/******************************* 添加文章 **************************************/
$(document).ready(function(e) {
    $("#add_news").click(function(){
		$("#news").show("fast");
		var p_id = $(".shouye_news_title").attr('name');
		$("#news form input[name='p_id']").val(p_id);
		getdatetime();
		$("#news form input[class='news_del']").css('display', 'none');
	});
});

$(document).ready(function(e) {
    $("#add_show").click(function(){
		$("#news").show("fast");
		var p_id = $(".shouye_show_title").attr('name');
		$("#news form input[name='p_id']").val(p_id);
		getdatetime();
		$("#news form input[class='news_del']").css('display', 'none');
	});
});

$(document).ready(function(e) {
    $("#add_enjoy").click(function(){
		$("#news").show("fast");
		var p_id = $(".shouye_enjoy_title").attr('name');
		$("#news form input[name='p_id']").val(p_id);
		getdatetime();
		$("#news form input[class='news_del']").css('display', 'none');
	});
});

/******************************* 控制生成图片的大小 **************************************/
$(document).ready(function(e) {
    $("#upload_news").click(function(){
		$("#upload_img").show("fast");
		$(".upload_title").html("新鲜事");
		var title = $(".shouye_news_title").html();
		var p_id = $(".shouye_news_title").attr('name');
		$(".upload_title").val(title);
		$("#upload_img form input[name='p_id']").val(p_id);
		$("#upload_img form input[name='width']").val(326);
		$("#upload_img form input[name='height']").val(251);
	});
});

$(document).ready(function(e) {
    $("#upload_show_type1").click(function(){
		$(".upload_title").html("大秀场");
		$("#upload_img").show("fast");
		var title = $(".shouye_show_title").html();
		var p_id = $(".shouye_show_title").attr('name');
		$(".upload_title").val(title);
		$("#upload_img form input[name='p_id']").val(p_id);
		$("#upload_img form input[name='type']").val(1);
		$("#upload_img form input[name='width']").val(373);
		$("#upload_img form input[name='height']").val(245);
	});
});

$(document).ready(function(e) {
    $("#upload_show_type2").click(function(){
		$(".upload_title").html("大秀场");
		$("#upload_img").show("fast");
		var title = $(".shouye_show_title").html();
		var p_id = $(".shouye_show_title").attr('name');
		$(".upload_title").val(title);
		$("#upload_img form input[name='p_id']").val(p_id);
		$("#upload_img form input[name='type']").val(2);
		$("#upload_img form input[name='width']").val(185);
		$("#upload_img form input[name='height']").val(120);
	});
});

$(document).ready(function(e) {
    $("#upload_show_type3").click(function(){
		$(".upload_title").html("大秀场");
		$("#upload_img").show("fast");
		var title = $(".shouye_show_title").html();
		var p_id = $(".shouye_show_title").attr('name');
		$(".upload_title").val(title);
		$("#upload_img form input[name='p_id']").val(p_id);
		$("#upload_img form input[name='type']").val(3);
		$("#upload_img form input[name='width']").val(208);
		$("#upload_img form input[name='height']").val(120);
	});
});


$(document).ready(function(e) {
    $("#upload_enjoy").click(function(){
		$(".upload_title").html("吃喝玩乐");
		$("#upload_img").show("fast");
		var title = $(".shouye_enjoy_title").html();
		var p_id = $(".shouye_enjoy_title").attr('name');
		$(".upload_title").val(title);
		$("#upload_img form input[name='p_id']").val(p_id);
		$("#upload_img form input[name='width']").val(215);
		$("#upload_img form input[name='height']").val(120);
	});
});


/******************************* 增加文章（json格式） **************************************/
$(document).ready(function(e) {
    $(".news_sub").click(function(){
		var title = $("#news form input[name='title']").val();
		var type = $("#news form input[name='type']").val();
		var author = $("#news form input[name='author']").val();
		var datetime = $("#news form input[name='datetime']").val();
		var image = $("#news form input[name='image']").val();
		var content = $("#news form textarea[name='content']").val();
		var p_id = $("#news form input[name='p_id']").val();
		var id = $("#news form input[name='id']").val();
		var data = "title=" + title + "&type=" + type + "&author=" + author + "&datetime=" + datetime + "&image=" + image + "&content=" + content + "&p_id=" + p_id + "&id=" + id;
		$.ajax({
			type:"POST",
			url:"addnews",
			data:data,
			beforesend:function(){},
			success:function(data){
				if(data != 0){
					alert("ok");
					flush();
				}
				else
					alert("error");
			}
		});
	});
});
/******************************* 修改文章 **************************************/
$(document).ready(function(e) {
    $(".contnet_list ul li").click(function(){
		$("#news form input[class='news_del']").css('display', 'inline');
		var id = $(this).attr('name');
		$.ajax({
			type:"POST",
			url:"getupdatedata",
			data:"id=" + id,
			beforesend:function(){},
			success:function(data){
				$("#news").show("fast");
				$("#news form input[name='title']").val(data.json_data.title);
				$("#news form input[name='type']").val(data.json_data.type);
				$("#news form input[name='author']").val(data.json_data.author);
				$("#news form input[name='datetime']").val(data.json_data.datetime);
				$("#news form input[name='image']").val(data.json_data.image);
				$("#news form textarea[name='content']").val(data.json_data.content);
				$("#news form input[name='p_id']").val(data.json_data.p_id);
				$("#news form input[name='id']").val(data.id);
			}
		});
	});
});
/******************************* 删除文章 **************************************/

$(document).ready(function(e) {
    $(".news_del").click(function(){
		var id = $("#news form input[name='id']").val();
		$.ajax({
			type:"POST",
			url:"delnews",
			data:"id=" + id,
			beforesend:function(){},
			success:function(data){
				if(data != 0){
					alert("ok");
					flush();
				}
				else
					alert("error");
			}
		});
	});
});

function getdatetime(){
	$.ajax({
		type:"POST",
		url:"getdatetime",
		dataType:"text",
		beforesend:function(){},
		success:function(data){
			$("#news form input[name='datetime']").val(data);		
		}
	});
}
function clear(){
	$(".edit_news").hide("fast");
	$("#news form input[name='title']").val('');
	$("#news form input[name='type']").val('');
	$("#news form input[name='author']").val('');
	$("#news form input[name='datetime']").val('');
	$("#news form input[name='image']").val('');
	$("#news form textarea[name='content']").val('');
	$("#news form input[name='p_id']").val('');
}
function flush(){
	var id = $("#news_management_title").attr("name");
	var url = $("#news_management_title").attr("title");
	$.ajax({
		type:"GET",
		url:url,
		data:"id=" + id,
		dataType:"html",
		beforeSend: function(){
		},
		success: function(data){
			$(".content").html(data);
		}
	});
}
